package com.shi.yibanjia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shi.yibanjia.entity.po.CarryFee;
import com.shi.yibanjia.entity.po.Order;
import com.shi.yibanjia.entity.po.TransportFee;
import com.shi.yibanjia.mapper.OrderMapper;
import com.shi.yibanjia.service.CarryFeeService;
import com.shi.yibanjia.service.OrderService;
import com.shi.yibanjia.service.TransportFeeService;
import com.shi.yibanjia.utils.TengxunLocationUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 20556
 * @description 针对表【tb_order(订单表)】的数据库操作Service实现
 * @createDate 2024-03-16 15:10:27
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
        implements OrderService {

    @Resource
    private TransportFeeService transportFeeService;

    @Resource
    private CarryFeeService carryFeeService;

    @Override
    public Double calcCarryPrice(Order order) {
        // 计算搬运费用
        CarryFee carryFee = carryFeeService.getOne(new LambdaQueryWrapper<CarryFee>().eq(CarryFee::getCarTypeId, order.getCarTypeId()));
        if (order.getNeedHelp().equals(1)) {
            return calcCarryFee(order.getSrcMoveWay(), order.getDescMoveWay(), order.getBigGoodsCount(), order.getSrcFloorLevel(), order.getDescFloorLevel(), carryFee);
        } else {
            return 0.0;
        }
    }

    @Override
    public Double calcTransPortPrice(Order order) {
        // 1. 计算运输费用
        // 1.1 获取公里数
        Double distance = TengxunLocationUtil.getDistanceByDriving(order.getSrcLatitude(), order.getSrcLongitude(), order.getDescLatitude(), order.getDescLongitude());
        TransportFee transportFee = transportFeeService.getOne(new LambdaQueryWrapper<TransportFee>().eq(TransportFee::getCarTypeId, order.getCarTypeId()));
        return calcTransportFee(distance, transportFee);
    }

    /**
     * 计算运输费用
     *
     * @param distance
     * @param transportFee
     * @return
     */
    private Double calcTransportFee(Double distance, TransportFee transportFee) {
        if (distance <= transportFee.getStartDistance()) {
            return transportFee.getStartFee();
        }
        // 剩余距离
        Double restDistance = distance - transportFee.getStartDistance();
        return transportFee.getStartFee() + restDistance * transportFee.getPerKmFee();
    }

    /**
     * 计算搬运费用
     *
     * @param srcMoveWay
     * @param descMoveMay
     * @param bigGoodsCount
     * @param srcFloorLevel
     * @param descFloorLevel
     * @param carryFee
     * @return
     */
    private Double calcCarryFee(Integer srcMoveWay, Integer descMoveMay, Integer bigGoodsCount, Integer srcFloorLevel, Integer descFloorLevel, CarryFee carryFee) {
        // 1. 大件物品搬运费
        Double bigGoodsCarryFee = carryFee.getBigGoodsCarryFee();
        if (srcMoveWay.equals(0)) {
            bigGoodsCarryFee += carryFee.getBigGoodsFloorCarryFee();
            if (descMoveMay.equals(0)) {
                bigGoodsCarryFee += carryFee.getBigGoodsFloorCarryFee();
            } else {
                if (descFloorLevel >= 2) {
                    bigGoodsCarryFee += (carryFee.getBigGoodsFloorCarryFee() * (descFloorLevel - 1));
                }
            }
        } else {
            if (srcFloorLevel >= 2) {
                bigGoodsCarryFee += (carryFee.getBigGoodsFloorCarryFee() * (srcFloorLevel - 1));
            }
            if (descMoveMay.equals(0)) {
                bigGoodsCarryFee += carryFee.getBigGoodsFloorCarryFee();
            } else {
                if (descFloorLevel >= 2) {
                    bigGoodsCarryFee += (carryFee.getBigGoodsFloorCarryFee() * (descFloorLevel - 1));
                }
            }
        }
        bigGoodsCarryFee = bigGoodsCarryFee * bigGoodsCount;
        // 2. 普通物品搬运费
        Double commonCarryFee = carryFee.getBaseCarryFee();
        if (srcMoveWay.equals(0)) {
            commonCarryFee += carryFee.getFloorCarryFee();
            if (descMoveMay.equals(0)) {
                commonCarryFee += carryFee.getFloorCarryFee();
            } else {
                if (descFloorLevel >= 2) {
                    commonCarryFee += (carryFee.getFloorCarryFee() * (descFloorLevel - 1));
                }
            }
        } else {
            if (srcFloorLevel >= 2) {
                commonCarryFee += (carryFee.getFloorCarryFee() * (srcFloorLevel - 1));
            }
            if (descMoveMay.equals(0)) {
                commonCarryFee += carryFee.getFloorCarryFee();
            } else {
                if (descFloorLevel >= 2) {
                    commonCarryFee += (carryFee.getFloorCarryFee() * (descFloorLevel - 1));
                }
            }
        }
        return bigGoodsCarryFee + commonCarryFee;
    }
}




